package com.tzw.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tzw.pojo.BasicCpjs;
import com.tzw.pojo.BasicKhal;
import com.tzw.pojo.BasicNews;
import com.tzw.pojo.ModuleOfProduct;
import com.tzw.service.BasicNewsService;
import com.tzw.service.ModuleOfProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 最新动态接口
 * */
@Controller
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private ModuleOfProductService moduleOfProductService;
    @Autowired
    private BasicNewsService basicNewsService;
    @GetMapping("/{page}")
    public String aboutView(Model model,
                            @PathVariable(value = "page")Integer page){
        List<ModuleOfProduct> productList = moduleOfProductService.list();
        QueryWrapper<BasicNews> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("time");
        IPage<BasicNews> newIPage = basicNewsService.page(new Page<>(page, 15),wrapper);
        model.addAttribute("prdList",productList);
        model.addAttribute("newList",newIPage.getRecords());
        model.addAttribute("page",newIPage.getPages());
        model.addAttribute("current",newIPage.getCurrent());
        return "/news";
    }
    @GetMapping("/info/{newId}")
    public String newInfoView(Model model,
                            @PathVariable(value = "newId")String newId){
        QueryWrapper<BasicNews> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",newId);
        List<ModuleOfProduct> productList = moduleOfProductService.list();
        List<BasicNews> list = basicNewsService.list(queryWrapper);
        for(BasicNews basicNews:list){
            String description = basicNews.getJs();
            basicNews.setJs(description.replaceAll("&","</br>"));
        }
        model.addAttribute("prdList",productList);
        model.addAttribute("newEntry",list);
        return "/newsInfo";
    }
}
